using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public enum JoystickType
{
    Normal,//固定摇杆
    CanChangePos,//可变位置摇杆
    CanMove,//可移动摇杆
}
public class JoystickController : MonoBehaviour
{
    //摇杆类型
    public JoystickType joystickType = JoystickType.Normal;
    //imgBGcontrol最大偏移范围
    public float maxOffset = 180f;
    private Image imgTouchRect;
    private Image imgBG;
    private Image imgBGcontrol;
    private Vector3 imgBGOldPos;

    // public UnityEvent<float> onJoystickMove;
    public UnityEvent<Vector3> onJoystickMove;
    //弹起事件
    public UnityEvent onJoystickUp;
    //按下事件
    public UnityEvent onJoystickDown;

    // Start is called before the first frame update
    void Start()
    {
        //获取ImgTouchRect的Image组件
        imgTouchRect = transform.Find("ImgTouchRect").GetComponent<Image>();
        imgBG = transform.Find("ImgTouchRect/ImgBG").GetComponent<Image>();
        imgBGcontrol = transform.Find("ImgTouchRect/ImgBG/ImgControl").GetComponent<Image>();

        //保存imgBG的初始位置
        imgBGOldPos = imgBG.rectTransform.localPosition;

        //可变位置摇杆时隐藏imgBGcontrol
        if (joystickType != JoystickType.Normal)
            imgBG.gameObject.SetActive(false); //隐藏imgBG


        //监听鼠标按下事件
        AddCustomEventListener(imgBG, EventTriggerType.PointerDown, OnTouchRectClick);
        //监听鼠标移动事件
        AddCustomEventListener(imgBG, EventTriggerType.Drag, OnTouchRectMove);
        //监听鼠标抬起事件
        AddCustomEventListener(imgBG, EventTriggerType.PointerUp, OnTouchRectUp);

    }

    /// <summary>
    /// 添加自定义事件监听器
    /// </summary>
    /// <param name="controller">控件对象</param>
    /// <param name="type">事件类型</param>
    /// <param name="callback">事件回调</param>
    private void AddCustomEventListener(UIBehaviour controller, EventTriggerType type, UnityAction<BaseEventData> callback)
    {
        EventTrigger trigger = controller.GetComponent<EventTrigger>();
        if(trigger == null)
            trigger = controller.gameObject.AddComponent<EventTrigger>();

        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = type;
        entry.callback.AddListener(callback);
        trigger.triggers.Add(entry);
    }

    //鼠标移动事件
    public void OnTouchRectMove(BaseEventData data)
    {
        // Debug.Log("OnTouchRectMove 移动");
        PointerEventData pointerData = (PointerEventData)data;
        Vector2 pos = pointerData.position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(imgBG.rectTransform, pos, pointerData.pressEventCamera, out pos);


        //获取imgTouchRect的子对象ImgBG
        imgBGcontrol.transform.localPosition = pos;

        //判断是否超出最大偏移范围
        if (pos.magnitude > maxOffset)
        {
            if (joystickType == JoystickType.CanMove)
                //超出多少就让背景图动多少
                imgBG.transform.localPosition += (Vector3)(pos.normalized * (pos.magnitude - maxOffset));
            //限制imgBGcontrol的偏移范围
            imgBGcontrol.transform.localPosition = pos.normalized * maxOffset;
        }

        // Debug.Log("移动 " +  pos.x / maxOffset);

        //转换成浮点数
        // float x = pos.x / maxOffset;

        //触发事件,传入参数
        // onJoystickMove.Invoke(pos.x / maxOffset);
        onJoystickMove.Invoke(pos / maxOffset);

    }
    //鼠标点击事件
    public void OnTouchRectClick(BaseEventData data)
    {
        onJoystickDown.Invoke();
        // Debug.Log("OnTouchRectClick 点击");
        //显示imgBG
        imgBG.gameObject.SetActive(true);

        //可变位置摇杆时隐藏imgBGcontrol
        if (joystickType != JoystickType.Normal)
        {
            PointerEventData pointerData = (PointerEventData)data;
            Vector2 pos = pointerData.position;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(imgBG.rectTransform, pos, pointerData.pressEventCamera, out pos);

            //设置imgBG的位置
            imgBG.rectTransform.position = pos;
        }


    }

    //鼠标抬起事件
    public void OnTouchRectUp(BaseEventData data)
    {
        // Debug.Log("OnTouchRectUp 释放");
        onJoystickUp.Invoke();

        //imgBG本地坐标归零
        imgBG.rectTransform.localPosition = imgBGOldPos;
        imgBGcontrol.rectTransform.localPosition = Vector3.zero;
        // onJoystickMove.Invoke(0f);
        onJoystickMove.Invoke(Vector3.zero);

        //可变位置摇杆时隐藏imgBGcontrol
        if (joystickType != JoystickType.Normal)
            imgBG.gameObject.SetActive(false);//隐藏imgBG

    }
    
}
